<template>
  <!-- 头部 -->
  <div class="header">
    <div class="item">
      <i class="iconfont icon-xiaozhushouhdpi" />
      <h1>涛涛记账</h1>
    </div>
    <card :msg="StatisticsInfo" />
    <iconCard :list="list" />
  </div>
  <!-- 主页列表 -->
  <div class="main">
    <ul
      v-for="bill in billList"
      :key="bill.id"
    >
      <li v-show="bill.list.length>0">
        <div>
          <span>{{ bill.time }}</span>&nbsp;&nbsp;
          <span>{{ bill.week }}</span>
        </div>
        <div>
          <span>收入：{{ bill.income }}</span>&nbsp;&nbsp;
          <span>支出：{{ bill.expenditure }}</span>
        </div>
      </li>
      <li
        v-for="item in bill.list"
        :key="item.id"
        class="list-item"
      >
        <div>
          <i
            class="iconfont"
            :class="[item.title ==='收入'? 'icon-shouru':'icon-benyuezhichu' ]"
            :style="{color: item.title ==='收入'? '#8B658B':'#f00'}"
          />
          <span> {{ item.title }}</span>
        </div>
        <span> {{ item.num }}</span>
      </li>
    </ul>
  </div>
</template>

<script setup name="Home">
import { onMounted, reactive, ref } from 'vue'
import { listInfo } from '@/api/public'
import { billInfon } from '@/api/user'
import { useStore } from 'vuex'
import { setItem } from '@/utils/token'

// 定义变量
const store = useStore()
const list = ref([])
const StatisticsInfo = reactive({})
const billList = ref({})

// 获取数据
onMounted(async () => {
  const result = await listInfo()
  if (result.code === 200) {
    list.value = result.data
  }
})

// 获取数据
onMounted(async () => {
  const result = await billInfon()
  if (result.code === 200) {
    // 添加判断请求的数据是否为游客数据
    billList.value = result.data.bills.reverse()
    Object.assign(StatisticsInfo, result.data.Statistics)
    store.dispatch('userBill', result.data)
    setItem('result', result)
  }
})
</script>

<style lang="less" scoped>
.header{
  width: 100%;
  height: 120px;
  margin-bottom: 35px;
  border-bottom-left-radius: 40%;
  border-bottom-right-radius: 40%;
  background-color:#8B658B;
  .item{
    width: 100%;
    height: 50px;
    display: flex;
   .icon-xiaozhushouhdpi{
     flex: 1;
     align-self: center;
     margin-left: 8%;
     font-size: 20px;
     color:deepskyblue;
   }
   h1{
     flex: 2;
     align-self: center;
     color: #000;
   }
  }
}
.main{
  width: 100%;
  margin-top: 45px;
  text-align: center;
  font-size: 20px;
  color: gray;
  ul{
    margin-top: 10px;
    li{
      &:nth-child(1) {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 25px;
        border-bottom: 1px solid rgba(128, 128, 128, 0.3);
        color: gray;
        font-size: 12px;
        padding: 0px 10px 0px 10px;
        // div{
        //   width: auto;
        //   display: flex;
        //   justify-content: space-between;
        // }
      }
    }
    li.list-item{
        width: 100%;
        height: 40px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        line-height: 40px;
        padding: 0px 10px 0px 10px;
        font-size: 13px;
        border-bottom: 1px solid rgba(128, 128, 128, 0.2);
        div{
          i{
            position: relative;
            top: 5px;
            font-size: 25px;
            margin-right: 10px;
          }
        }
      }
  }
}
</style>
